﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Belyn.Common.Web;

namespace CartoonViewer
{
    /// <summary>
    /// UC_DownloadListPanel.xaml 的交互逻辑
    /// </summary>
    public partial class UC_DownloadListPanel : UserControl
    {
        public UC_DownloadListPanel()
        {
            InitializeComponent();
            this.MouseLeftButtonDown += new MouseButtonEventHandler(UC_DownloadListPanel_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new MouseButtonEventHandler(UC_DownloadListPanel_MouseLeftButtonUp);
            this.MouseMove += new MouseEventHandler(UC_DownloadListPanel_MouseMove); 
            
        } 

        System.Collections.Generic.Dictionary<string, UC_DownloadItem> DownloadList = new Dictionary<string, UC_DownloadItem>();

        public void AddItem(UC_DownloadItem newItem)
        {
            if (newItem == null || this.DownloadList.ContainsKey(newItem.Title) == true)
            {
                return;
            }
              
            this.DownloadList.Add(newItem.Title, newItem);
            
            ControlHelper.SetValue(this.PlList, new Action(() =>
            {
                newItem.DownloadListPanel = this;
                 
                this.PlList.Children.Add(newItem);
                newItem.MouseLeftButtonUp += new MouseButtonEventHandler(UC_DownloadItem_MouseLeftButtonUp);
                
            })); 
        } 

        public void RemoveItem(UC_DownloadItem item)
        {
            if (item == null || this.DownloadList.ContainsKey(item.Title) == false)
            {
                return;
            }

            this.DownloadList.Remove(item.Title);

            ControlHelper.SetValue(this.PlList, new Action(() => {
                this.PlList.Children.Remove(item);  
            }));

            this.RemoveSelectedItem(item);

        }

        public void RemoveItem(string title)
        {
            if (this.DownloadList.ContainsKey(title) == false)
            {
                return;
            }

            this.RemoveItem(this.DownloadList[title]); 
        }

        public bool ExistsItem(UC_DownloadItem item)
        {
            if (item == null || this.ExistsItem(item.Title) == false)
            {
                return false;
            }

            return true;
        }

        public bool ExistsItem(string title)
        {
            if (this.DownloadList.ContainsKey(title) == false)
            {
                return false;
            } 
            return true;
        }

        public UC_DownloadItem GetItem(string title)
        {  
            return this.DownloadList[title];
        }

        //Ctrl键是否按下了
        public bool CtrlKeyIsPress
        {
            get
            { 
                return Keyboard.Modifiers == ModifierKeys.Control;
            }
        }

        public List<UC_DownloadItem> SelectedItems = new List<UC_DownloadItem>();
        public void AddSelectedItem(UC_DownloadItem item)
        {
            if (this.SelectedItems.Contains(item) == true ) return;

            if (this.CtrlKeyIsPress == false)
            {
                this.ClearSelectedItem();
            }
             
            this.SelectedItems.Add(item);
        }

        public void RemoveSelectedItem(UC_DownloadItem item)
        {
            if (this.SelectedItems.Contains(item) == false) return;

            this.SelectedItems.Remove(item);
             
        }

        public void ClearSelectedItem()
        {
            foreach (UC_DownloadItem i in SelectedItems)
            {
                i.SetSelected(false);
            }
            this.SelectedItems.Clear();
        }

        void UC_DownloadItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            UC_DownloadItem item = sender as UC_DownloadItem;

            if (item.IsSelected == true)
            {
                item.SetSelected(false);
                this.RemoveSelectedItem(item);
            }
            else
            {
                item.SetSelected(true);
                this.AddSelectedItem(item);
            }

        }


        /*
            矩形拖动框选择 
        */

        #region 矩形拖动框选择 属性

        Point BeginPoint;
        Point EndPoint;
        bool IsMouseDowning;
       

        #endregion 
        void UC_DownloadListPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.IsMouseDowning = false;

            if (this.PanelDraw != null)
            {
                this.PanelDraw.ReleaseMouseCapture();
                if (this.PanelDraw.Children.Contains(RectDraw) == true)
                {
                    this.PanelDraw.Children.Remove(RectDraw);
                }
                
                PanelDraw.Visibility = Visibility.Hidden;
            }

            AfterMoved();

            //回收资源
            GC.Collect();
            
            
        }

        void UC_DownloadListPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDowning == false) return;

            if (this.PanelDraw.IsMouseCaptured == false)
            {
                this.PanelDraw.CaptureMouse();
            }
            

            EndPoint = e.GetPosition(this.PanelDraw);

            DrawRectangle(BeginPoint, EndPoint); 

        }



        void UC_DownloadListPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.IsMouseDowning = true;

            if (this.PanelDraw.Children.Contains(RectDraw) == false)
            {
                RectDraw.Stroke = Brushes.Black;
                DoubleCollection dc = new DoubleCollection();
                dc.Add(1);
                dc.Add(2);
                RectDraw.StrokeDashArray = dc ; 
                RectDraw.Width = 0;
                RectDraw.Height = 0;
                this.PanelDraw.Children.Add(RectDraw);
            }
             
            this.BeginPoint = e.GetPosition(this.PlList);
            this.MoveSelectedItem.Clear();
            PanelDraw.Visibility = Visibility.Visible;
        }

        void DrawRectangle(Point begin, Point end)
        {
            this.DrawRectangle(begin, end, this.PanelDraw);
        }
        System.Windows.Shapes.Rectangle RectDraw = new Rectangle();
        Point EndPosition = new Point(); 
        Point BeginPosition = new Point();
        Rect RectSelected = new Rect();
        void DrawRectangle(Point begin, Point end,FrameworkElement container)
        { 
            
            double actualWidth,actualHeight;
            actualWidth = Math.Abs(begin.X - end.X);
            actualHeight = Math.Abs(begin.Y - end.Y);

            RectDraw.Width = actualWidth;
            RectDraw.Height = actualHeight;

            Point position = BeginPosition;

            if (this.BeginPoint.Y == this.EndPoint.Y)
            {
                position.Y = this.BeginPoint.Y;
                if (this.BeginPoint.X >= this.EndPoint.X)
                {
                    position.X = this.BeginPoint.X;
                }
                else
                {
                    position.X = this.EndPoint.X;
                }
            }
            else
            {
                double k = (this.EndPoint.Y - this.BeginPoint.Y) / (this.EndPoint.X - this.BeginPoint.X);
                if (k >= 0)
                {
                    if (this.BeginPoint.X >= this.EndPoint.X)
                    {
                        position.X = this.EndPoint.X;
                        position.Y = this.EndPoint.Y;
                        
                    }
                    else
                    {
                        position.X = this.BeginPoint.X;
                        position.Y = this.BeginPoint.Y;
                    }
                }
                else
                { 
                    if (this.BeginPoint.X >= this.EndPoint.X)
                    {
                        position.X = this.EndPoint.X;
                        position.Y = this.BeginPoint.Y;
                    }
                    else
                    {
                        position.X = this.BeginPoint.X;
                        position.Y = this.EndPoint.Y;
                    }
                }
                
                 
            }

            EndPosition.X  = position.X + actualWidth;
            EndPosition.Y = position.Y + actualHeight;
 

            RectDraw.SetValue(Canvas.LeftProperty, position.X);
            RectDraw.SetValue(Canvas.TopProperty, position.Y);

            RectSelected.Location = position;
            RectSelected.Size = new Size(actualWidth, actualHeight);

            SetSelected(RectSelected);
        }

        
        List<UC_DownloadItem> MoveSelectedItem = new List<UC_DownloadItem>();
        void SetSelected(Rect r)
        {  
            Point o = new Point(0.0,0.0);
             
            foreach (UC_DownloadItem item in this.DownloadList.Values)
            {
                Rect rItem = new Rect(item.TranslatePoint(o, this.PlList), new Size(item.ActualWidth, item.ActualHeight));
                bool isContain = rItem.IntersectsWith(r);

                if (isContain == true)
                {
                    item.SetSelected(true);
                    this.MoveSelectedItem.Add(item); 
                }
                else
                {
                    item.SetSelected(false);
                    if (this.MoveSelectedItem.Contains(item) == true)
                    {
                        this.MoveSelectedItem.Remove(item);
                    }
                   
                }

             }
           
            
        }


        void AfterMoved()
        {
            if (this.CtrlKeyIsPress == true)
            {
                foreach (UC_DownloadItem item in this.MoveSelectedItem)
                {
                    if (this.SelectedItems.Contains(item))
                    {
                        this.SelectedItems.Remove(item);
                    }
                    else
                    {
                        this.SelectedItems.Add(item);
                    }
                }
            }
            else
            {
                this.SelectedItems.Clear();
                this.SelectedItems.AddRange(this.MoveSelectedItem.AsEnumerable());
            }
        }


    }
}
